package com.huaruan.youdi.views;

import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.huaruan.youdi.utils.DeviceUtils;

public class WHImageView extends AppCompatImageView {

    public WHImageView(Context context) {
        super(context);
    }

    public WHImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WHImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void load(String url, int height, int width) {
        ViewGroup.LayoutParams para = getLayoutParams();
        para.height = height;
        para.width = width;
        Glide.with(getContext()).load(url).into(this);
    }

    public void load(String url) {
        setScaleType(ScaleType.CENTER_CROP);
        Glide.with(getContext()).load(url).into(this);
    }

    public void loadRound(String url,int round) {
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(DeviceUtils.dp2px(round));
        //解决CenterCrop与圆角冲突 导致圆角失效问题
        MultiTransformation multiTransformation = new MultiTransformation<>(new CenterCrop(), roundedCorners);
        //通过RequestOptions扩展功能
        RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA)
                .bitmapTransform(multiTransformation);
        Glide.with(getContext())
                .load(url)
                .apply(options)
                .into(this);
    }

    public void loadLocal(int id) {
        Glide.with(getContext()).load(id).into(this);
    }

    //加载圆形图片
    public void loadCirclePic(String url) {
        Glide.with(getContext())
                .load(url)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(this);

    }

    //加载圆形图片
    public void loadLocalCirclePic(int id) {
        Glide.with(getContext())
                .load(id)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(this);

    }

}
